﻿namespace AnyOffice.Components
{
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.Caching;
    using System.Reflection;

    public class InterfaceCache
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static void Update()
        {
            try
            {
                Hashtable table = new Hashtable();
                string str = "SELECT [IeTitle],[StatusText],[BannerText],[BannerFont],[LogoImg],[LogoWidth],[LogoHeight],[LoginImg] FROM [dbo].[Interface]";
                SqlDataReader reader = SqlHelper.ExecuteReader(ConfigManager.ConnectionString, CommandType.Text, str);
                if (reader.Read())
                {
                    table["IeTitle"] = !reader.IsDBNull(0) ? reader.GetString(0) : string.Empty;
                    table["StatusText"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                    table["BannerText"] = !reader.IsDBNull(2) ? reader.GetString(2) : string.Empty;
                    table["BannerFont"] = !reader.IsDBNull(3) ? reader.GetString(3) : string.Empty;
                    table["LogoImg"] = !reader.IsDBNull(4) ? reader.GetString(4) : string.Empty;
                    table["LogoWidth"] = !reader.IsDBNull(5) ? reader.GetInt32(5) : 100;
                    table["LogoHeight"] = !reader.IsDBNull(6) ? reader.GetInt32(6) : 40;
                    table["LoginImg"] = !reader.IsDBNull(7) ? reader.GetString(7) : string.Empty;
                }
                reader.Close();
                table = Hashtable.Synchronized(table);
                HttpContext.Current.Cache.Insert("AnyOffice_Interface", table, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                //log.IsErrorEnabled

                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static string BannerFont
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return ((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["BannerFont"].ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return string.Empty;
                }
            }
        }

        public static string BannerText
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return ((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["BannerText"].ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return string.Empty;
                }
            }
        }

        public static string IeTitle
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return ((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["IeTitle"].ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return string.Empty;
                }
            }
        }

        public static string LoginImg
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return ((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["LoginImg"].ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return string.Empty;
                }
            }
        }

        public static int LogoHeight
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["LogoHeight"].ToString());
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return 40;
                }
            }
        }

        public static string LogoImg
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return ((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["LogoImg"].ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return string.Empty;
                }
            }
        }

        public static int LogoWidth
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["LogoWidth"].ToString());
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return 100;
                }
            }
        }

        public static string StatusText
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Cache["AnyOffice_Interface"] == null) Update();
                    return ((Hashtable) HttpContext.Current.Cache["AnyOffice_Interface"])["StatusText"].ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return string.Empty;
                }
            }
        }
    }
}

